package com.bndg.bndggame.httprequest;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by r on 18/4/12.
 */

public class RetrofitManager {
    private static RetrofitManager retrofitManager = new RetrofitManager();
    private Retrofit retrofit;
    public static RetrofitManager getInstance() {
        return retrofitManager;
    }

    private RetrofitManager() {
        initRetrofit();
    }

    private void initRetrofit() {
        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
        LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(new RspCheckInterceptor()); //添加检查拦截器
        if (true){
            builder.addInterceptor(LoginInterceptor); //添加retrofit日志打印
        }


        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();


        retrofit = new Retrofit.Builder()
                .baseUrl("http://123.207.50.74/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
    }

    public <T> T createReq(Class<T> reqServer){
        return retrofit.create(reqServer);
    }

}
